
import Vue from 'vue'
Vue.mixin({
    data(){
        return {
            dataSource: [],
            allStatus: [],
            pgSize: 10, //页数
            
        }
    },

    methods: {
        GMTToStr : function(time){
            let date = new Date(time)
            let Str=date.getFullYear() + '-' +
                    (date.getMonth() + 1) + '-' +
                    date.getDate() + ' ' +
                    date.getHours() + ':' +
                    date.getMinutes() + ':' +
                    date.getSeconds()
            return Str
        },
        StrToGMT(time){
            let GMT = new Date(time)
            return GMT
        },
        search(searchData,baseURL){
            //判断所有的搜索属性是否为空,如果为空,则显示所有数据
           var isAllEmpty =  this.isAllEmpty(searchData);
           var searchURL;
            if(isAllEmpty){
                searchURL = baseURL;
            }else{
                for(var key in searchData){
                    if(searchData[key] !=null && String(searchData[key]).length!=0){
                        searchURL= baseURL+key+"="+searchData[key];
                        break; 
                    }
                }
            }
            searchURL=searchURL+"&pageSize=5&pageNum=1"
           return this.$http.get(searchURL)
            .then((response)=>{
                if(response.data.data.list.results){
                    this.dataSource = response.data.data.list.results;
                    this.totalRecord = response.data.data.list.totalRecord;
                    return response;
                }else{
                    this.dataSource = [];
                }
            })
            .catch((error)=>{
            })
        },
        // 在搜索框获取焦点的时候,将其他搜索框的内容全部清空
        clearAnotherInput(searchType){
            for(var key in this.searchData){
                if(key!=searchType){
                    this.searchData[key]=''
                }
            }
            this.currentPage = 1;
            this.loadData(1);
        },
        isAllEmpty(searchData){
            //如果有一个属性不为空,则返回false
            for(var key in searchData){
                if(key!=='baseURL'&&searchData[key] !=null && String(searchData[key]).length!=0){
                    return false;
                }
            }
            return true;
        },
        // 根据状态编号过滤数据
        searchByStatus(searchValue){
            if(searchValue>=3){
                return;
            }
            var searchURL = window.baseURL + "/backstage/social/findByPage?status="+searchValue+"&token="+this.$token;
            this.dataSource = this.tempDataSource;
            this.$http.get(searchURL)
            .then((response)=>{
                if(response.data.data.list.results){
                    this.dataSource = response.data.data.list.results
                }
            })
        },
        checkData(checkedDdataArra,data){
            var unPush = false;
            var emptyValue = '';
            checkedDdataArra.forEach((value)=>{
                if(data[value]==null || String(data[value]).trim().length == 0){
                    emptyValue = value;
                    unPush = true;
                    return;
                }
            })
             if(unPush){
                 this.$alert(emptyValue+'为空请填写必要的数据', '无法提交', {
                    confirmButtonText: '确定',
                    callback: action => {
                        this.$message({
                        type: 'info',
                        message: `action: ${ action }`
                        });
                    }

             });
                return false;
            }
            return true
        },
        loadDataWithURL(url){
           
              this.$http.get(url)
            .then((response)=>{
                this.dataSource = response.data.data.list.results;
            })
            .catch((error)=>{
                console.log(error);
            })
        },
        loadDataWithType(type){
            /*
            type: 
                education 
                occupation 
                social_status
                source
            */
            var sourceURL = window.baseURL + "/backstage/config/findConfigs?ktype=" +type +"&token=" +this.$token;
            this.$http.get(sourceURL)
            .then(response => {
                this[type] = response.data.data;
                localStorage.setItem(type,JSON.stringify(response.data.data))
            })
            .catch(error => {
                console.log(error);
            });
        },
        // 对各种操作,进行二次确认,避免误操作
        operatorConfirm(action,text){
            this.$confirm(`请确认是否继续${text}?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                var promise = action();
              }).catch(() => {
                this.$message({
                  type: 'info',
                  message: `已经取消${text}`
                });          
              });
        },
        //加载城市列表
        loadCity(cityURL){
           var cityPromise = this.$http.get(cityURL)
            .then((response)=>{
                return response;
            })
            .catch((error)=>{
                console.log(error);
            })
            return cityPromise;
        },
        loadAllCampus(cityId){
            var campusURL = window.baseURL + "//backstage/campus/findByCity?token="+this.$token+"&cityId="+cityId;
            return  this.$http.get(campusURL)
            .then((response)=>{
                return response;
            })
            .catch((error)=>{
                console.log(error);
            })
        },
    },
   
})
export default {}